package com.appjishu.web.biz.service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.appjishu.web.biz.dao.ScoreBoardMapper;
import com.appjishu.web.biz.dao.UserMapper;
import com.appjishu.web.model.ScoreBoard;
import com.appjishu.web.model.User;

@Service
public class ScoreBoardService {
    @Autowired
    private ScoreBoardMapper scoreBoardMapper;
    @Autowired
    private UserMapper userMapper;

    public List<ScoreBoard> rankingList(LocalDateTime dateBegin, LocalDateTime dateEnd){
        List<ScoreBoard> scoreboard = scoreBoardMapper.rankingList(dateBegin, dateEnd);
        Set<Long> uids = scoreboard.stream().map(ScoreBoard::getUserId).collect(Collectors.toSet());
        Map<Long, User> id2u = new HashMap<>();
        if(!uids.isEmpty()) {
            id2u = userMapper.selectBatchIds(uids).stream().collect(Collectors.toMap(User::getId, t->t));
        }
        Map<Long, User> finalId2u = id2u;
        scoreboard.forEach(item -> {
        	BeanUtils.copyProperties(finalId2u.get(item.getUserId()), item);
        });
        return scoreboard;
    }

    public List<ScoreBoard> rankingListByCondition(List<User> condition) {
        List<ScoreBoard> scoreboard = scoreBoardMapper.rankingListByCondition(condition);
        return scoreboard;
    }
}
